<?php
namespace app\home\controller;

use app\home\logic\LoginLogic;
use app\home\logic\SiteConfigLogic;
use app\home\model\NavModel;
use app\home\util\Response;
use Qiniu\Auth;
use think\Cache;
use think\Config;
use think\Controller;
use think\Cookie;
use think\Request;
use think\Session;

class Base extends Controller
{
    protected $_check_login = [];
    protected $_user_id;
    public function __construct(Request $request = null)
    {
        $controller = strtolower(Request::instance()->controller());
        $action = strtolower(Request::instance()->action());
        parent::__construct($request);
        $nav = new NavModel();
        $nav_list = $nav->get_nav_list();
        $this->assign('nav_list',$nav_list);
        $logic = new SiteConfigLogic();
        $logic->get_site_config();
        $this->assign('site',Cache::get('site'));
        $login = new LoginLogic();
        if($login->check_login() === false){//登录失败
            if(in_array($action,$this->_check_login)){
                if(Request::instance()->isAjax()){
                    Response::json(-2, '请先登录');
                }else{
                    $this->redirect(url('home/login/index'));
                }
            }
        }
        $this->_user_id = Session::get('user.id');
        $user_info = Cookie::get('user');
        if($user_info){
            $this->assign('user_info',$user_info);
        }else{
            $this->assign('user_info',false);
        }
        $this->assign('controller',$controller);
        $this->assign('version','v3');
    }
    protected function qiniu_start(){
        $accesskey = Config::get('site.qiniu_accesskey');
        $secretKey = Config::get('site.qiniu_secretkey');
        $auth = new Auth($accesskey,$secretKey);
        $bucket = Config::get('site.qiniu_name');
        $this->assign('domain',Config::get('site.qiniu_domin'));
        $token = $auth->uploadToken($bucket,null,3600,['mimeLimit'=>'image/*;video/*']);
        $this->assign('token',$token);
    }
}
